package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetPswFindActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.img_eye)
    ImageView imgEye;
    private boolean isSee=false;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_psw_find);
        ButterKnife.bind(this);
        phone=getIntent().getStringExtra("phone");
        initTitle();
    }

    private void initTitle() {
        tvMiddleTitle.setText("修改密码");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm, R.id.img_eye})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                String yzm = etYzm.getText().toString();
                String psw = etPsw.getText().toString();
                if (TextUtils.isEmpty(yzm) || TextUtils.isEmpty(psw)) {
                    ToastUtils.toastForShort(ResetPswFindActivity.this, "验证码和密码不能为空");
                    return;
                }
                confirm(yzm, psw);
                break;
            case R.id.img_eye:
                if(isSee){
                    etPsw.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    isSee=false;
                    imgEye.setBackgroundResource(R.mipmap.eye);
                }else{
                    etPsw.setInputType(InputType.TYPE_CLASS_TEXT);
                    isSee=true;
                    imgEye.setBackgroundResource(R.mipmap.eye_open);
                }
                break;
        }
    }

    private void confirm(String yzm, String psw) {
        OkGo.<String>post(Constant.USER)
                .tag(this)
                .params("code", yzm)
                  /*   1、注册 2、找回密码*/
                .params("pwd", psw)
                .params("mobile", phone)
                .params("action", "back_pwd")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dr=new DealResult();
//                        BasicBean bean=dr.dealBasic(ResetPswFindActivity.this,response);
//                        if(bean==null){
//                            return;
//                        }
//                        if (Constant.SUCESS == bean.getStatus()) {
//                            ToastUtils.toastForLong(ResetPswFindActivity.this, "修改密码成功！");
//                            ResetPswFindActivity.this.finish();
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForLong(ResetPswFindActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
